import logging
from opslog.cli.plugins import BasePlugin
from opslog.cli.data import DataManager

class Auth(BasePlugin):
    def setup_arguments(self,root_subparsers):
        auth_parser = root_subparsers.add_parser("auth",help="Authentication commands")
        auth_subparsers = auth_parser.add_subparsers()
    
        setup_auth_parser = auth_subparsers.add_parser("setup",help="Initialize authentication")
        setup_auth_parser.set_defaults(run=self.auth_setup)
    
        check_auth_parser = auth_subparsers.add_parser("check",help="Check current authentication status")
        check_auth_parser.set_defaults(run=self.auth_check)
    
        forget_auth_parser = auth_subparsers.add_parser("forget",help="Forget authentication credentials")
        forget_auth_parser.set_defaults(run=self.auth_forget)
 
    def auth_setup(self):
        self.client.auth_setup()

    def auth_forget(self):
        DataManager.remove("auth","access_token")
        self.logger.info("Authentication redentials cleared.")

    def auth_check(self):
        if self.client.auth_check():
            self.logger.info("Authentication ok")
        else:
            self.logger.info("Invalida authentication credentials.Run with `auth setup` arguments to fix it.")
